package com.rayo.accesscontrollercopy.module.login;

import android.content.Context;

import com.litesuits.common.utils.HexUtil;
import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.model.db.bean.KeyUserData;
import com.rayo.accesscontrollercopy.model.db.bean.OperatorData;
import com.rayo.accesscontrollercopy.model.db.dao.CurrentUserDao;
import com.rayo.accesscontrollercopy.model.db.dao.CurrentUserImpl;
import com.rayo.accesscontrollercopy.model.db.dao.KeyUserDao;
import com.rayo.accesscontrollercopy.model.db.dao.KeyUserImpl;
import com.rayo.accesscontrollercopy.model.db.dao.OperatorDao;
import com.rayo.accesscontrollercopy.model.db.dao.OperatorImpl;
import com.rayo.accesscontrollercopy.utils.NetWorkUtils;
import com.rayo.wallreader.jni.WallreaderJni;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${Adam} on 15/11/12.
 */
public class LoginPresenter {
    private ILoginView iLoginView;
    private Context mContext;
    private OperatorDao mOperatorDao;
    private KeyUserDao mKeyUserDao;
    private String keyId;
    private CurrentUserDao mCurrentUserDao;
    public static  final  int ERROR=-1;
    public static  final  int OK=0;
    public static  final  int OTHER=1;
    public LoginPresenter(Context context, ILoginView loginView) {
        this.mContext = context;
        this.iLoginView = loginView;
        GlobalVariables.getInstance().setAndroidId(NetWorkUtils.getAndroidId(mContext));
        this.mOperatorDao = new OperatorImpl();
        this.mKeyUserDao=new KeyUserImpl();
        this.mCurrentUserDao=new CurrentUserImpl();
    }

    public String getKeyId() {
        return keyId;
    }

    public void setKeyId(String keyId) {
        this.keyId = keyId;
    }

    public int  login(int action) {

            if(action!=LoginActivity.OPEN) {
                List<OperatorData> loginAndPwd = new ArrayList<>();
                if (iLoginView.getLoginName().isEmpty() || iLoginView.getPassword().isEmpty()) {
                    loginAndPwd = mOperatorDao.findAll();
                    if (null == loginAndPwd || loginAndPwd.size() == 0) return OK;
                    else return ERROR;
                }
                loginAndPwd = mOperatorDao.findByLoginAndPwd(iLoginView.getLoginName());
                if (null == loginAndPwd || !(loginAndPwd.size() > 0)) return ERROR;
                byte[] pwd = HexUtil.decodeHex(loginAndPwd.get(0).getPassword().toCharArray());
                byte[] code = new byte[pwd.length];
                WallreaderJni.getmInstance().des(1, pwd, pwd.length, code);
                byte[] p = iLoginView.getPassword().getBytes();
                if (p.length > pwd.length)
                    return ERROR;
                for (int i = 0; i < p.length; i++) {
                    if (p[i] != code[i])
                        return ERROR;
                }
                return OK;
            }
        else {
                keyId="";
                if (iLoginView.getLoginName().isEmpty() || iLoginView.getPassword().isEmpty()) {
                    return ERROR;
                }
                KeyUserData keyUserData=mKeyUserDao.findByLogin(iLoginView.getLoginName().toString());
                if (null == keyUserData ) return ERROR;
                byte[] pwd = HexUtil.decodeHex(keyUserData.getPassword().toString().toCharArray());
                byte[] code = new byte[pwd.length];
                WallreaderJni.getmInstance().des(1, pwd, pwd.length, code);
                byte[] p = iLoginView.getPassword().getBytes();
                if (p.length > pwd.length) return ERROR;
                for (int i = 0; i < p.length; i++) {
                    if (p[i] != code[i]) return ERROR;
                }
//                if(mCurrentUserDao.findByUserId(keyUserData.getUserid())!=null){
//                    return OTHER;
//                }
                keyId=keyUserData.getUserid();
                return OK;
            }

//
    }

}
